

clear
insheet using  "data/raw/BlindLeadingBlind_Birmingham21Oct2015.csv", name
drop in 1
save "data/tempData1.dta", replace

clear
insheet using  "data/raw/BlindLeadingBlind_Birmingham22Oct2015.csv", name
drop in 1
append using "data/tempData1.dta"
save "data/tempData1.dta", replace

clear
insheet using  "data/raw/BlindLeadingBlind_Birmingham23Oct2015.csv", name
drop in 1
append using "data/tempData1.dta"
save "data/tempData1.dta", replace

clear
insheet using  "data/raw/BlindLeadingBlind_Birmingham28Oct2015.csv", name
drop in 1
append using "data/tempData1.dta"
save "data/tempData1.dta", replace

clear
insheet using  "data/raw/BlindLeadingBlind_Birmingham29Oct2015.csv", name
drop in 1
append using "data/tempData1.dta"
save "data/tempData1.dta", replace

clear
insheet using  "data/raw/BlindLeadingBlind_Birmingham30Oct2015.csv", name
drop in 1
append using "data/tempData1.dta"
save "data/tempData1.dta", replace

clear


insheet using  "data/raw/BlindLeadingBlind_Birmingham15Feb2016.csv", name
drop in 1
save "data/tempDataW2.dta", replace

clear
insheet using  "data/raw/BlindLeadingBlind_Birmingham16Feb2016.csv", name
drop in 1
append using "data/tempDataW2.dta"
save "data/tempDataW2.dta", replace

clear
insheet using  "data/raw/BlindLeadingBlind_Birmingham17Feb2016.csv", name
drop in 1
append using "data/tempDataW2.dta"
save "data/tempDataW2.dta", replace

clear
insheet using  "data/raw/BlindLeadingBlind_Birmingham18Feb2016.csv", name
drop in 1
append using "data/tempDataW2.dta"
save "data/tempDataW2.dta", replace

clear
insheet using  "data/raw/BlindLeadingBlind_Birmingham23Feb2016.csv", name
drop in 1
append using "data/tempDataW2.dta"
save "data/tempDataW2.dta", replace

clear
insheet using  "data/raw/BlindLeadingBlind_Birmingham24Feb2016.csv", name
drop in 1
append using "data/tempDataW2.dta"
save "data/tempDataW2.dta", replace




********* String datasets together 

if 1 {

	clear
	insheet using  "data/raw/BlindLeadingBlindBirmingham6May2016.csv", name
	drop in 1/2
	capture: drop q127*
	compress
	save "data/rawSpring1.dta", replace

	clear
	insheet using  "data/raw/BlindLeadingBlindBirmingham9May2016.csv", name
	drop in 1/2
	capture: drop q127*
	compress
	save "data/rawSpring2.dta", replace

	clear
	insheet using  "data/raw/BlindLeadingBlindBirmingham10May2016.csv", name
	drop in 1/2
	capture: drop q127*
	compress
	save "data/rawSpring3.dta", replace

	clear
	insheet using  "data/raw/BlindLeadingBlindBirmingham11May2016.csv", name
	drop in 1/2
	capture: drop q127*
	compress
	save "data/rawSpring4.dta", replace

	clear
	insheet using  "data/raw/BlindLeadingBlindBirmingham12May2016.csv", name
	drop in 1/2
	capture: drop q127*
	compress
	save "data/rawSpring5.dta", replace
	
	
	clear 
	use "data/tempDataW2.dta"
	rename v8 startdate
	rename v9 enddate
	save "data/tempDataW2.dta", replace
	
	clear
	use "data/tempData1.dta"
	rename v8 startdate
	rename v9 enddate
	save "data/tempData2.dta", replace
	
	
	clear
	use "data/rawSpring1.dta"
	append using "data/rawSpring2.dta", force
	append using "data/rawSpring3.dta", force
	append using "data/rawSpring4.dta", force
	append using "data/rawSpring5.dta", force
	
	// qualtrics has a system variable named status that conflicts with our survey variable status. The following two lines solve this. 
	drop status
	rename v1834 status

	append using "data/tempDataW2.dta", force
	append using "data/tempData1.dta", force

	destring, replace	
	compress
	
	save "data/rawAll.dta", replace

}
	
	


clear
use "data/rawAll.dta"	
********* Drop session etc. 

if 1 {
		
	drop if session==26 | session==27 | session==28								// drop bad controls in winter -- communication unannounced
	replace session=session-2 if session>26 & session<=30 & treatment!=.
	gen id = idown+((session-1)*50)

	replace pairinfo=6 if (idown==4 & session==23) | (idown==3 & session==25) //Due to a coding mistake in the data file
	
	replace treatment=3 if treatment==.
	replace treatment=0 if treatment==2
	gen control=1-treatment
	gen nocommunication=treatment==3

	// treatments
	// 0 	paired, communication, no partner educated
	// 1	paired, communication, sender is educated
	// 3	no communication, communication announced (newest control)

	// Make "bad control" a new treatment (need to manage session names, since two sessions labelled 30, one in winter, one in spring)

}

********* Session start times 	
	
replace startdate="2015-10-21 10:00:00" if session==3
replace startdate="2015-10-21 14:00:00" if session==4
replace startdate="2015-10-22 10:00:00" if session==5
replace startdate="2015-10-22 14:00:00" if session==6
replace startdate="2015-10-23 10:00:00" if session==7
replace startdate="2015-10-23 14:00:00" if session==8
replace startdate="2015-10-28 09:00:00" if session==9
replace startdate="2015-10-28 12:00:00" if session==10
replace startdate="2015-10-28 15:00:00" if session==11
replace startdate="2015-10-29 09:00:00" if session==12
replace startdate="2015-10-29 12:00:00" if session==13
replace startdate="2015-10-29 15:00:00" if session==14
replace startdate="2015-10-30 09:00:00" if session==15
replace startdate="2015-10-30 12:00:00" if session==16
replace startdate="2015-10-30 15:00:00" if session==17
replace startdate="2016-02-15 10:00:00" if session==18
replace startdate="2016-02-15 14:00:00" if session==19
replace startdate="2016-02-16 10:00:00" if session==20
replace startdate="2016-02-16 14:00:00" if session==21
replace startdate="2016-02-17 10:00:00" if session==22
replace startdate="2016-02-17 14:00:00" if session==23
replace startdate="2016-02-18 10:00:00" if session==24
replace startdate="2016-02-18 14:00:00" if session==25
replace startdate="2016-02-24 10:00:00" if session==27
replace startdate="2016-02-24 14:00:00" if session==28
replace startdate="2016-05-06 10:00:00" if session==30
replace startdate="2016-05-06 14:00:00" if session==31
replace startdate="2016-05-09 10:00:00" if session==32
replace startdate="2016-05-09 14:00:00" if session==33
replace startdate="2016-05-10 10:00:00" if session==34
replace startdate="2016-05-10 14:00:00" if session==35
replace startdate="2016-05-11 10:00:00" if session==36
replace startdate="2016-05-11 14:00:00" if session==37
replace startdate="2016-05-12 14:00:00" if session==38

		
gen double startTime = clock(startdate, "YMDhms")
gen double endTime = clock(enddate, "YMDhms")

//change endTime to reflect the time difference between UK and US. Time difference is 8 hours for most sessions but 7 hours for Oct 28-Oct30 sessions because of the US switches to winter time 1 week after UK. 
replace endTime=endTime+8*1000*60*60 if session<9 | session>17
replace endTime=endTime+7*1000*60*60 if session>=9 & session<=17

gen double duration = endTime - startTime
replace duration = duration / (1000*60) // duration in minutes

********* Variable naming

if 1 {
		
		
	gen sender=1 if role==1
	replace sender=0 if role==2 | role==.

	gen receiver=1-sender

	gen educated=0
	replace educated=1 if sender==1 & treatment==1

	replace age=age+17

	gen white=ethnicity==1
	gen asian=ethnicity==2
	gen black=ethnicity==3
	gen mixed=ethnicity==4
	gen chinese=ethnicity==5
	gen other=ethnicity==6

	foreach x in 1 2 6 8 10 12 13 14 15{
	replace q125_`x'=q125_q125_`x' if treatment==3
	}
	gen sum= q125_1+q125_2+q125_15+q125_6+q125_8+q125_10+q125_12+q125_13+q125_14
	gen understandingCheck1=0
	replace understandingCheck1=1 if sum==14 | sum==15
	gen understandingCheck2=0
	replace understandingCheck2=1 if q160==5

	// understandingCheck1: prefer more money to less in MPL
	// understandingCheck2: can read MPL




	gen followup=followupe if educated==1
	replace followup=followupu if educated!=1
	// do people agree to be followed up?

	gen heardBeforeRule72=be4rule72e 		if educated==1
	replace heardBeforeRule72=be4rule72u 	if educated~=1

	gen attemptTeachRule72=attrule72e 		if educated==1
	replace attemptTeachRule72=attrule72u 	if educated!=1

	gen successTeachToRule72=sucrule72e 	if educated==1
	replace successTeachToRule72=sucrule72u if educated~=1

	gen useRule72C=userule72e 			if educated==1
	replace useRule72C=userule72u 			if educated~=1

	gen abiRule72C=abirule72e 			if educated==1
	replace abiRule72C=abbrule72u 			if educated~=1
	// see block "Debriefing" in qualtrics survey

}



***** reorder variables such that the switch points correspond to the same decisions for each subject

if 1 {
	reshape long switch1longfirst switch1longsecond switch1longthird ///
		switch2longfirst switch2longsecond switch2longthird ///
		ordermplfirstlong ordermplsecondlong ordermplthirdlong ///
		evaloptfirst evaloptsecond evaloptthird ///
		compoptfirst compoptsecond compoptthird, i(id) j(decisionOrder)

	rename ordermplfirstlong decisionNumber1
	rename ordermplsecondlong decisionNumber2
	rename ordermplthirdlong decisionNumber3

	rename switch1longfirst switch1_1
	rename switch1longsecond switch1_2
	rename switch1longthird switch1_3

	rename switch2longfirst switch2_1
	rename switch2longsecond switch2_2
	rename switch2longthird switch2_3

	rename evaloptfirst  evalopt1
	rename evaloptsecond evalopt2
	rename evaloptthird  evalopt3

	rename compoptfirst   compopt1
	rename compoptsecond  compopt2
	rename compoptthird   compopt3


	gen id2 = 1000*id + decisionOrder

	reshape long switch1_ switch2_ decisionNumber evalopt compopt, i(id2) j(part)

	rename  switch1_ switch1
	rename  switch2_ switch2

	drop if switch1==.
	drop id2

	gen tmpl=.
	forvalues x=1/9 {
	replace tmpl=tmpl11_3`x'+ tmpl12_3`x' if decisionNumber==`x'
	}
	forvalues x=10/24 {
	local j=`x'-9
	replace tmpl=tmpls21_3`j'+ tmpls22_3`j' if sender==1 & decisionNumber==`x'
	}
	forvalues x=16/24 {
	local j=`x'-9
	replace tmpl=tmplr21_3`j'+ tmplr22_3`j' if sender==0 & decisionNumber==`x'
	}
	forvalues x=25/30 {
	local j=`x'-24
	replace tmpl=tmplr21_3`j'+ tmplr22_3`j' if sender==0 & decisionNumber==`x'
	}

	forvalues x=25/42 {
	local j=`x'-24
	replace tmpl=tmpls31_3`j'+ tmpls32_3`j' if sender==1 & decisionNumber==`x'
	}
	forvalues x=10/15 {
	local j=`x'-9
	replace tmpl=tmplr31_3`j'+ tmplr32_3`j' if sender==0 & decisionNumber==`x'
	}
	forvalues x=31/42 {
	local j=`x'-24
	replace tmpl=tmplr31_3`j'+ tmplr32_3`j' if sender==0 & decisionNumber==`x'
	}


	****** Enter the properties of the choice options here
	gen delay = 48
	replace delay = 72 if (decisionNumber == 1 | decisionNumber == 2 | decisionNumber == 3 ///
		| decisionNumber == 10 | decisionNumber == 11 | decisionNumber == 12 /// 
		| decisionNumber == 16 | decisionNumber == 17 | decisionNumber == 18 /// 
		| decisionNumber == 25 | decisionNumber == 26 | decisionNumber == 27 /// 
		| decisionNumber == 31 | decisionNumber == 32 | decisionNumber == 33 ///
		| decisionNumber == 37 | decisionNumber == 38 | decisionNumber == 39) 

	replace delay = 36 if  decisionNumber == 7 | decisionNumber==22
	replace delay = 54 if  decisionNumber == 8 | decisionNumber==23
	replace delay = 18 if  decisionNumber == 9 | decisionNumber==24


	gen comparisonTime =0 
	// set to 1 for FV
	replace comparisonTime=1 if (decisionNumber >= 7 & decisionNumber<=9) 
	replace comparisonTime=1 if (decisionNumber >= 22 & decisionNumber<=24) 

	// set to fv as calculated by the rule of 72
	gen fv72 = .
	replace fv72=26 if decisionNumber == 1 
	replace fv72=59 if decisionNumber == 2 
	replace fv72=90 if decisionNumber == 3 | decisionNumber == 21
	replace fv72=58 if decisionNumber == 5 | decisionNumber == 17 | decisionNumber == 41
	replace fv72=89 if decisionNumber == 6
	replace fv72=25 if decisionNumber == 16 | decisionNumber == 40
	replace fv72=92 if decisionNumber == 18
	replace fv72=57 if decisionNumber == 20 | decisionNumber == 38
	replace fv72=91 if decisionNumber == 39 | decisionNumber == 42

	replace fv72=24 if decisionNumber == 4 | decisionNumber == 7 | decisionNumber == 10 | decisionNumber == 13 ///
		| decisionNumber == 19 | decisionNumber == 24 | decisionNumber == 25 | decisionNumber == 28 ///
		| decisionNumber == 31 | decisionNumber == 34 |decisionNumber==37

	replace fv72=56 if decisionNumber == 8 | decisionNumber == 11 | decisionNumber == 14 ///
		| decisionNumber == 22 | decisionNumber == 26 | decisionNumber == 29 | decisionNumber == 32 | decisionNumber==35
		
	replace fv72=88 if decisionNumber == 9 | decisionNumber == 12 | decisionNumber == 15 ///
		| decisionNumber == 23 | decisionNumber == 27 | decisionNumber == 30 | decisionNumber == 33 | decisionNumber==36




	// set to actual fv
	gen fv = .
	replace fv=26 if decisionNumber == 1 
	replace fv=59 if decisionNumber == 2 
	replace fv=90 if decisionNumber == 3 | decisionNumber == 21
	replace fv=24 if decisionNumber == 4 | decisionNumber == 19 | decisionNumber == 37
	replace fv=58 if decisionNumber == 5 | decisionNumber == 17 | decisionNumber == 41
	replace fv=89 if decisionNumber == 6
	replace fv=25 if decisionNumber == 16 | decisionNumber == 40
	replace fv=92 if decisionNumber == 18
	replace fv=57 if decisionNumber == 20 | decisionNumber == 38
	replace fv=91 if decisionNumber == 39 | decisionNumber == 42


	/*
	replace fv=24.6236 if decisionNumber == 7
	replace fv=58.1967 if decisionNumber == 8
	replace fv=87.9124 if decisionNumber == 9

	replace fv=24.9668 if decisionNumber == 10
	replace fv=58.2560 if decisionNumber == 11
	replace fv=91.5451 if decisionNumber == 12
	replace fv=24.5217 if decisionNumber == 13 
	replace fv=57.2174 if decisionNumber == 14 
	replace fv=89.9130 if decisionNumber == 15

	replace fv=57.0308 if decisionNumber == 22
	replace fv=91.2799 if decisionNumber == 23
	replace fv=24.3098 if decisionNumber == 24

	replace fv=25.2001 if decisionNumber == 25
	replace fv=58.8001 if decisionNumber == 26
	replace fv=92.4002 if decisionNumber == 27
	replace fv=24.7935 if decisionNumber == 28
	replace fv=57.8515 if decisionNumber == 29
	replace fv=90.9095 if decisionNumber == 30

	replace fv=24.5652 if decisionNumber == 31
	replace fv=57.3188 if decisionNumber == 32
	replace fv=90.0724 if decisionNumber == 33
	replace fv=24.8144 if decisionNumber == 34
	replace fv=57.9002 if decisionNumber == 35
	replace fv=90.9860 if decisionNumber == 36
	*/

	replace fv=25 if decisionNumber == 7 | decisionNumber == 10 | decisionNumber == 13 | decisionNumber == 25 | decisionNumber == 28 | decisionNumber == 31 | decisionNumber == 34
	replace fv=58 if decisionNumber == 8 | decisionNumber == 11 | decisionNumber == 29 | decisionNumber == 35
	replace fv=88 if decisionNumber == 9
	replace fv=92 if decisionNumber == 12 | decisionNumber == 27
	replace fv=57 if decisionNumber == 14 | decisionNumber == 22 | decisionNumber == 32
	replace fv=90 if decisionNumber == 15 | decisionNumber == 33
	replace fv=91 if decisionNumber == 23 | decisionNumber == 30 | decisionNumber == 36
	replace fv=24 if decisionNumber == 24
	replace fv=59 if decisionNumber == 26

	save "data/data.dta", replace

	// decisionType is 0 for PV, 1 for FV, 2 for time 

	gen decisionType = 0 
	replace decisionType = 1 if (decisionNumber >= 7 & decisionNumber<=9) | (decisionNumber >= 22 & decisionNumber<=24)
	replace decisionType = 2 if (decisionNumber >= 1 & decisionNumber<=6) | (decisionNumber >= 16 & decisionNumber<=21) | (decisionNumber >= 37 & decisionNumber<=42)

	gen PV = decisionType == 0
	gen FV = decisionType == 1
	gen timePrefs = decisionType == 2

	gen part1 = part == 1
	gen part2 = part == 2
	gen part3 = part == 3

	// normalize choices by actual future value

	gen switchNormal = switch2 / fv

	gen sqDiff = .
	gen absDiff = .
	replace sqDiff = (switchNormal - 1)^2 if FV == 1
	replace absDiff = sqrt(sqDiff)

}	
	
**********************

//Matching Questions

// create paired valuation problems (long data)
// "group" almost always refers to a pair of a complex and the equivalent simply framed problem

gen group=.
gen i=0
forvalues j=10/15{
replace i=i+1
replace group=i if (decisionNumber==`j' | decisionNumber==`j'+6) & sender==1
}
forvalues j=25/30{
replace i=i+1
replace group=i if (decisionNumber==`j' | decisionNumber==`j'+12) & sender==1
}

forvalues j=25/30{
replace i=i+1
replace group=i if (decisionNumber==`j' | decisionNumber==`j'-9) & sender==0
}
forvalues j=10/15{
replace i=i+1
replace group=i if (decisionNumber==`j' | decisionNumber==`j'+27) & sender==0
}


gen groupN=.
replace i=0
forvalues j=31/36{
replace i=i+1
replace groupN=i if (decisionNumber==`j' | decisionNumber==`j'+6)
}

// look at Excel file "The blind leading the blind/Design/BLB treatment selection V6"
// groupN is for non-discussed questions



gen discussed=.
replace discussed=0 if decisionNumber>30 & decisionNumber<37
replace discussed=1 if decisionNumber>9 & decisionNumber<16 & receiver==1

gen groupD=.
replace i=0
forvalues j=10/15{
replace i=i+1
replace groupD=i if  (decisionNumber==`j' | decisionNumber==`j'+21) & sender==0
}

// groupD pairs complexly framed discussed questions with complexly framed non-discussed questions


gen groupAmb=. // stands for groupAmbiguous
replace i=0
forvalues j=16/21{
replace i=i+1
replace groupAmb=i if  (decisionNumber==`j' | decisionNumber==`j'+21)
}
// this groups simply framed quesions in part 2 with simply framed questions in part 3 to do Bernheim Rangel analysis allowing for ambiguity in welfare-relevant domain


*********************
egen pairID = group(pairinfo session)


sort id
by id: egen ownTimePrefsMean = mean(switchNormal) if part <= 2 & timePrefs == 1
by id: egen ownTimePrefsMean1 = mean(switchNormal) if part == 1 & timePrefs == 1
by id: egen ownTimePrefsMean2 = mean(switchNormal) if part == 2 & timePrefs == 1

by id: egen ownTimePrefsMedian = median(switchNormal) if part <= 2 & timePrefs == 1

*gender=1 for men
*ethinicity=1 for African-American, 2 for Asian, 3 for Caucasian, 4 for Hispanic and 5 for other
sort pairID
by pairID: egen maxTimePrefsMean = max(ownTimePrefsMean) if pairID!=.
by pairID: egen minTimePrefsMean = min(ownTimePrefsMean) if pairID!=.

by pairID: egen maxTimePrefsMean1 = max(ownTimePrefsMean1) if pairID!=.
by pairID: egen minTimePrefsMean1 = min(ownTimePrefsMean1) if pairID!=.

by pairID: egen maxTimePrefsMean2 = max(ownTimePrefsMean2) if pairID!=.
by pairID: egen minTimePrefsMean2 = min(ownTimePrefsMean2) if pairID!=.

by pairID: egen maxTimePrefsMedian = max(ownTimePrefsMedian) if pairID!=.
by pairID: egen minTimePrefsMedian = min(ownTimePrefsMedian) if pairID!=.

by pairID: egen maxGender = max(gender) if pairID!=.
by pairID: egen minGender = min(gender) if pairID!=.

by pairID: egen maxEducated = max(educated) if pairID!=.
by pairID: egen minEducated = min(educated) if pairID!=.

by pairID: egen maxEthnicity = max(ethnicity) if pairID!=.
by pairID: egen minEthnicity = min(ethnicity) if pairID!=.

gen partnerTimePrefsMean = .
gen partnerTimePrefsMean1 = .
gen partnerTimePrefsMean2 = .
gen partnerTimePrefsMedian = .
gen partnerGender = .
gen partnerEducated = .
gen partnerEthnicity=.


replace partnerTimePrefsMean = minTimePrefsMean if ownTimePrefsMean == maxTimePrefsMean
replace partnerTimePrefsMean = maxTimePrefsMean if ownTimePrefsMean == minTimePrefsMean

replace partnerTimePrefsMean1 = minTimePrefsMean1 if ownTimePrefsMean1 == maxTimePrefsMean1
replace partnerTimePrefsMean1 = maxTimePrefsMean1 if ownTimePrefsMean1 == minTimePrefsMean1
replace partnerTimePrefsMean2 = minTimePrefsMean2 if ownTimePrefsMean2 == maxTimePrefsMean2
replace partnerTimePrefsMean2 = maxTimePrefsMean2 if ownTimePrefsMean2 == minTimePrefsMean2

replace partnerTimePrefsMedian = minTimePrefsMedian if ownTimePrefsMedian == maxTimePrefsMedian
replace partnerTimePrefsMedian = maxTimePrefsMedian if ownTimePrefsMedian == minTimePrefsMedian

replace partnerGender = minGender if gender == maxGender
replace partnerGender = maxGender if gender == minGender

replace partnerEducated = minEducated if educated == maxEducated
replace partnerEducated = maxEducated if educated == minEducated

replace partnerEthnicity = minEthnicity if ethnicity == maxEthnicity
replace partnerEthnicity = maxEthnicity if ethnicity == minEthnicity


sort id
by id: egen ownTimePrefsMean_2 = max(ownTimePrefsMean)
by id: egen ownTimePrefsMean1_2 = max(ownTimePrefsMean1)
by id: egen ownTimePrefsMean2_2 = max(ownTimePrefsMean2)
by id: egen partnerTimePrefsMean_2 = max(partnerTimePrefsMean)
by id: egen partnerTimePrefsMean1_2 = max(partnerTimePrefsMean1)
by id: egen partnerTimePrefsMean2_2 = max(partnerTimePrefsMean2)
by id: egen ownTimePrefsMedian_2 = max(ownTimePrefsMedian)
by id: egen partnerTimePrefsMedian_2 = max(partnerTimePrefsMedian)
by id: egen partnerGender_2 = max(partnerGender)
by id: egen partnerEducated_2 = max(partnerEducated)
by id: egen partnerEthnicity_2 = max(partnerEthnicity)

drop ownTimePrefsMean ownTimePrefsMean1 ownTimePrefsMean2 partnerTimePrefsMean ///
	partnerTimePrefsMean1 partnerTimePrefsMean2  ownTimePrefsMedian partnerTimePrefsMedian ///
	partnerGender partnerEducated partnerEthnicity 

rename ownTimePrefsMean_2 		ownTimePrefsMean 
rename ownTimePrefsMean1_2 		ownTimePrefsMean1 
rename ownTimePrefsMean2_2 		ownTimePrefsMean2
 
rename partnerTimePrefsMean_2  	partnerTimePrefsMean 
rename partnerTimePrefsMean1_2  partnerTimePrefsMean1 
rename partnerTimePrefsMean2_2  partnerTimePrefsMean2 

rename ownTimePrefsMedian_2  	ownTimePrefsMedian 
rename partnerTimePrefsMedian_2 partnerTimePrefsMedian

rename partnerGender_2 			partnerGender
rename partnerEthnicity_2 		partnerEthnicity
rename partnerEducated_2 		partnerEducated				


gen sameGender = gender == partnerGender
gen sameEthnicity = ethnicity ==partnerEthnicity


sort id
egen tagId = tag(id)

rename q148 partnerGraspAbsolute
rename q150 partnerBetterGraspThanMe
rename q140_3 workoutStartTime

replace partnerGraspAbsolute = comppartne_1 if treatment ~= 3
replace partnerBetterGraspThanMe = comparison_1 if treatment ~= 3


 keep  decisionNumber decisionOrder part id session pairinfo pairID tagId treatment role gender age ethnicity urban educ big* conformity* ///
 crt* finlit* disctime* tagId  switch2 absDiff switchNormal tmpl ///
 part3 part2 part1 timePrefs FV PV fv fv72 group groupN groupD groupAmb comparisonTime delay educated receiver sender control ///
 followup suggestion preparehow_5_text preparehow_5 prepare* talk imprule72 heardBeforeRule72 friends* discussed discussed_1 dischelped_1 sqDiff ///
 compselg comppartne comparison_1 namrule72u namrule72u_text attemptTeachRule72 successTeachToRule72 useRule72C abiRule72C ///
 userule72b abirule72b abirul72a nativelang status inter major gpa finassista* cardown cashadvanc cardroll hhsize hhincome understandingCheck1 understandingCheck2 ///
 ownTimePrefsMean  ownTimePrefsMean1  ownTimePrefsMean2  partnerTimePrefsMean  partnerTimePrefsMean1  partnerTimePrefsMean2   ownTimePrefsMedian  partnerTimePrefsMedian  partnerGender partnerEthnicity partnerEducated ///
 partnerGraspAbsolute partnerBetterGraspThanMe workoutStartTime email duration
  
 // to include additional variables (e.g. timing), look at "Birmingham-Winter 2016/data" then in one of tose do files (maybe "diagnostics")
 
 
 
 ****** Data management parts that were in analysis file
 
 
 	by id, sort: egen maxSwitchNormal=max(switchNormal) if FV==1 & part1==1
	by id, sort: egen minSwitchNormal=min(switchNormal) if FV==1 & part1==1
	gen knowledge=0
	replace knowledge=1 if maxSwitchNormal<1.1 & minSwitchNormal>.9
	by id, sort: egen knowledgeable=max(knowledge)

	// normalization is as if every true future value was equal 1 (subject answer divided by true FV)	
	
	egen questionGroup=group(group id)
	bysort questionGroup: egen maxDelta = max(switchNormal) if FV==0 & part1==0 & discussed!=0
	by questionGroup: egen minDelta = min(switchNormal) if FV==0 & part1==0 & discussed!=0
	
	
	gen deltaSimple=.
	replace deltaSimple=maxDelta if switchNormal==minDelta & PV==1 & discussed!=0
	replace deltaSimple=minDelta if switchNormal==maxDelta & PV==1 & discussed!=0

	gen deltaComplex=.
	replace deltaComplex=minDelta if switchNormal==minDelta & PV==1 & discussed!=0
	replace deltaComplex=maxDelta if switchNormal==maxDelta & PV==1 & discussed!=0

	egen questionGroupN=group(groupN id)
	bysort questionGroupN: egen maxDeltaN = max(switchNormal) if FV==0 & part3==1 & discussed!=1
	bysort questionGroupN: egen minDeltaN = min(switchNormal) if FV==0 & part3==1 & discussed!=1

	replace deltaSimple=maxDeltaN if switchNormal==minDeltaN & PV==1 & discussed==0
	replace deltaSimple=minDeltaN if switchNormal==maxDeltaN & PV==1 & discussed==0

	replace deltaComplex=minDeltaN if switchNormal==minDeltaN & PV==1 & discussed==0
	replace deltaComplex=maxDeltaN if switchNormal==maxDeltaN & PV==1 & discussed==0

	egen questionGroupAmb=group(groupAmb id)
	bysort questionGroupAmb: egen maxDeltaAmb = max(switchNormal) if timePrefs==1 & part>1
	bysort questionGroupAmb: egen minDeltaAmb = min(switchNormal) if timePrefs==1 & part>1

	bysort questionGroup: egen maxDeltaAmbC = max(maxDeltaAmb) if FV==0 & discussed!=0
	bysort questionGroup: egen minDeltaAmbC = max(minDeltaAmb) if FV==0 & discussed!=0
	bysort questionGroupN: egen maxDeltaAmbCN = max(maxDeltaAmb) if FV==0 & part3==1 & discussed!=1
	bysort questionGroupN: egen minDeltaAmbCN = max(minDeltaAmb) if FV==0 & part3==1 & discussed!=1

	gen maxAbsDiffDelta=max(abs(switchNormal-maxDeltaAmbC), abs(switchNormal-minDeltaAmbC)) if (PV==1 & part3==1 & (discussed==1 | discussed==.)) | (PV==1 & part2==1)
	gen minAbsDiffDelta=min(abs(switchNormal-maxDeltaAmbC), abs(switchNormal-minDeltaAmbC)) if (PV==1 & part3==1 & (discussed==1 | discussed==.)) | (PV==1 & part2==1)
	replace maxAbsDiffDelta=max(abs(switchNormal-maxDeltaAmbCN), abs(switchNormal-minDeltaAmbCN)) if PV==1 & part3==1 & discussed==0
	replace minAbsDiffDelta=min(abs(switchNormal-maxDeltaAmbCN), abs(switchNormal-minDeltaAmbCN)) if PV==1 & part3==1 & discussed==0


	gen absDiffDelta=.
	replace absDiffDelta=abs(deltaComplex-deltaSimple)

	gen DiffDelta=.
	replace DiffDelta=deltaComplex-deltaSimple

	gen DiffDeltaSquared=.
	replace DiffDeltaSquared=DiffDelta^2

	gen diffLogDelta=.
	replace diffLogDelta=log(deltaComplex)-log(deltaSimple)

	gen absDiffLogDelta=.
	replace absDiffLogDelta=abs(diffLogDelta)

	egen questionGroupD=group(groupD id)
	bysort questionGroupD: egen diffDelta = max(switchNormal) if FV==0 & part3==1 & timePrefs==0


	bysort questionGroupD: egen maxDeltaD = max(switchNormal) if FV==0 & part3==1 & timePrefs==0
	bysort questionGroupD: egen minDeltaD = min(switchNormal) if FV==0 & part3==1 & timePrefs==0

	gen deltaDiscussed=.
	replace deltaDiscussed=maxDeltaD if switchNormal==minDeltaD & PV==1 & discussed==0
	replace deltaDiscussed=minDeltaD if switchNormal==maxDeltaD & PV==1 & discussed==0

	gen deltaNotDiscussed=.
	replace deltaNotDiscussed=minDeltaD if switchNormal==minDeltaD & PV==1 & discussed==0
	replace deltaNotDiscussed=maxDeltaD if switchNormal==maxDeltaD & PV==1 & discussed==0

	gen DiffDeltaD=deltaDiscussed-deltaNotDiscussed
	gen absDiffDeltaD=abs(deltaDiscussed-deltaNotDiscussed)
	gen DiffLogDeltaD= log(deltaDiscussed)-log(deltaNotDiscussed)
	gen absDiffLogDeltaD= abs(DiffLogDeltaD)


	gen educated_part2=educated*part2
	gen educated_part3=educated*part3
	gen treatment_part3=treatment*part3
	gen uneducated=1-educated
	


	gen finLit1=1 if finlit1==1
	replace finLit1=0 if finlit1==2 | finlit1==3 | finlit1==4

	gen finLit2=1 if finlit2==3
	replace finLit2=0 if finlit2==1 | finlit2==2 | finlit2==4

	gen finLit3=1 if finlit3==2
	replace finLit3=0 if finlit3==1 | finlit3==3

	gen finLit=0 
	replace finLit=1 if finLit1==1 & finLit2==1 & finLit3==1

	
	// bottom / middle / top measures whether the true FV is at the bottom (low amount) / middle / top (high amount) of the MPL
	
	gen bottom=0
	replace bottom=1 if decisionNumber==10 | decisionNumber==13 | decisionNumber==25 | decisionNumber==28 | decisionNumber==31 | decisionNumber==34

	gen middle=0
	replace middle=1 if decisionNumber==11 | decisionNumber==14 | decisionNumber==26 | decisionNumber==29 | decisionNumber==32 | decisionNumber==35

	gen top=0
	replace top=1 if decisionNumber==12 | decisionNumber==15 | decisionNumber==27 | decisionNumber==30 | decisionNumber==33 | decisionNumber==36

	
	// psychological conformity scale
	gen conformity=conformity_4-conformity_5+conformity_6+conformity_7+conformity_9+conformity_8-conformity_10+conformity_11-conformity_12+conformity_13-conformity_14

	egen tagID=tag(id)
	
	gen Crt1=0
	replace Crt1=1 if crt1=="$ 0.05" | crt1=="$0.05" | crt1=="0.05" | crt1=="0.05 $" | crt1=="0.05$" | crt1=="0.05 dollars" | crt1=="0.05 pound" | crt1=="5" | crt1=="5 cents" | crt1=="5 pence" | crt1=="5p" | crt1=="Â£0.05"


	gen Crt2=0
	replace Crt2=1 if crt2=="5" | crt2=="5 minuets" | crt2=="5 minuites" | crt2=="5 minutes"

	gen Crt3=0
	replace Crt3=1 if crt3=="47" | crt3=="47 Days" | crt3=="47 days"

	gen cogAbility=Crt1+Crt2+Crt3

	label define major 2 "Arts and Humanities" 3 "Business and Economics" 4 "Engineering and Computer Science" 5 "Health and Rehabilitation Sciences" 6 "Social Sciences" 7 "Other Sciences" 8 "Other"
	label values major major

	label define income 1 "Less than £10,000" 2 "£10,000-£24,999" 3 "£25,000-£34,999" 4 "£35,000-£49,999" 5 "£50,000-£74,999" 6 "£75,000-£100,000" 7 "More than £100,000" 8 "Do not know" 9 "Prefer not to say"
	label values hhincome income

	label define eth 1 "White" 2 "Asian" 3 "Black" 4 "Mixed" 5 "Other" 6 "Chinese"
	label values ethnicity eth 
	gen white=ethnicity==1

	label define gender 1 "Male" 0 "Female"
	label values gender gender
	gen male=gender==1

	rename urban community
	label define comm 1 "Urban" 2 "Suburban" 3 "Rural"
	label values community comm 

	rename inter international
	replace international=0 if international==2
	label define inter 0 "National" 1 "International"
	label values international inter

	gen part2NC=part2==1 & treatment==3
	gen part2C=part2==1 & treatment==0
	gen part3NC=part3==1 & treatment==3
	gen part3C=part3==1 & treatment==0


	// whether Fulya ran the session
	// note: if treatment == 3, then session ran by Dan
	gen Fulya=0
	replace Fulya=1 if session<18

 
 
 

saveold "data/mainData.dta", replace

run "doFiles/dataManagementTranscription.do"




/*

replace absDiff 		= absDiff^2
replace absDiffDelta 	= absDiffDelta^2
replace absDiffDeltaD 	= absDiffDeltaD^2
replace absDiffDeltaND 	= absDiffDeltaND^2
replace absDiffPart2 	= absDiffPart2^2


saveold "data/mainDataSq.dta", replace

*/

